﻿using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using eProject.ViewModel;
using eProject.ViewModel.Commands;

namespace eProject.View
{
    /// <summary>
    /// Interaction logic for TasksView.xaml
    /// </summary>
    public partial class TasksView : INotifyPropertyChanged
    {
        public TasksView()
        {
            InitializeComponent();
            RefreshMode();            
        }

        public enum Modes
        {
            Left, Right
        }

        private Modes _mode = Modes.Left;

        public Modes Mode
        {
            get { return _mode; }
            set
            {
                if (_mode == value) return;
                _mode = value;
                RefreshMode();
                InvokePropertyChanged("Mode");
            }
        }

        public static readonly DependencyProperty ManagerProperty = DependencyProperty.Register("Manager",
                                                                                                               typeof(
                                                                                                                   CommonManager
                                                                                                                   ),
                                                                                                               typeof(
                                                                                                                   TasksView)
                                                                                                              );

        public static readonly DependencyProperty CommandsQueueProperty =
            DependencyProperty.Register("CommandsQueue", typeof(CommandsQueue), typeof(TasksView));

        public static readonly DependencyProperty OwnerWindowProperty =
           DependencyProperty.Register("OwnerWindow", typeof(Window), typeof(TasksView));

        public CommandsQueue CommandsQueue
        {
            get { return (CommandsQueue)GetValue(CommandsQueueProperty); }
            set { SetValue(CommandsQueueProperty, value); }
        }

        public CommonManager Manager
        {
            get { return (CommonManager)GetValue(ManagerProperty); }
            set { SetValue(ManagerProperty, value); }
        }

        
        public Window OwnerWindow
        {
            get { return (Window)GetValue(OwnerWindowProperty); }
            set { SetValue(OwnerWindowProperty, value); }
        }

        private void RefreshMode()
        {
            switch (Mode)
            {
                case Modes.Left:
                    IsUntouchable = false;
                    ShowButtons = true;
                    listBox.Background = Brushes.LightGray;
                    break;
                case Modes.Right:
                    IsUntouchable = true;
                    ShowButtons = false;
                    listBox.Background = FindResource("grayStripedBrush") as Brush;
                    break;
            }
        }

        public bool ShowButtons { 
            get { return buttonsPanel.Visibility == Visibility.Visible; }
            set
            {
                if (value == (buttonsPanel.Visibility == Visibility.Visible)) return;
                buttonsPanel.Visibility = value ? Visibility.Visible : Visibility.Hidden;
                InvokePropertyChanged("ShowButtons");//TODO remove event?
            }
        }

        public bool IsUntouchable
        {
            get { return rectangle.Visibility == Visibility.Visible; }
            set
            {
                if (value == (rectangle.Visibility == Visibility.Visible)) return;
                rectangle.Visibility = value ? Visibility.Visible : Visibility.Hidden;
                InvokePropertyChanged("IsUntouchable");//TODO remove event?
            }
        }

        public event SelectionChangedEventHandler SelectionChanged
        {
            add { listBox.SelectionChanged += value; }
            remove { listBox.SelectionChanged -= value;}
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void InvokePropertyChanged(string property)
        {
            PropertyChangedEventHandler changed = PropertyChanged;
            if (changed != null) changed(this, new PropertyChangedEventArgs(property));
        }

        private void CommandBinding_DeleteCommand(object sender, ExecutedRoutedEventArgs e)
        {            
            var item = listBox.SelectedItem;
            var task = item as TaskViewModel;
            if (task == null) return; //TODO log
            CommandsQueue.Do(new DeleteTaskCommand(Manager, task));
        }

        private void CommandBinding_CreateCommand(object sender, ExecutedRoutedEventArgs e)
        {
            var dialog = new CreateTaskDialog(Manager){Owner = OwnerWindow};
            var result = dialog.ShowDialog();
            if (result == true)
            {
                var name = dialog.TaskName;
                var descr = dialog.TaskDescription;
                var duration = dialog.TaskDuration;
                CommandsQueue.Do(new CreateTaskCommand(Manager, name, descr, duration));
            }
        }

        private void CommandBinding_DeleteCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (listBox == null) return;            
            var item = listBox.SelectedItem;
            var task = item as TaskViewModel;
            e.CanExecute = task != null;
        }

        private void CommandBinding_MoveUpCommand(object sender, ExecutedRoutedEventArgs e)
        {
            var index = listBox.SelectedIndex;
            CommandsQueue.Do(new SwitchItemsCommand<TaskViewModel>(Manager.SwitchTasks, Manager.Tasks[index], Manager.Tasks[index - 1]));
        }

        private void CommandBinding_MoveUpCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (listBox == null) return;
            var index = listBox.SelectedIndex;
            e.CanExecute = index > 0;
        }

        private void CommandBinding_MoveDownCommand(object sender, ExecutedRoutedEventArgs e)
        {
            var index = listBox.SelectedIndex;
            CommandsQueue.Do(new SwitchItemsCommand<TaskViewModel>(Manager.SwitchTasks, Manager.Tasks[index], Manager.Tasks[index + 1]));
        }

        private void CommandBinding_MoveDownCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (listBox == null) return;
            var index = listBox.SelectedIndex;
            e.CanExecute = index < listBox.Items.Count - 1 && index >= 0;
        }

        private void listBox_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Keyboard.Focus(listBox);
        }

        private void CommandBinding_EditCommand(object sender, ExecutedRoutedEventArgs e)
        {
            var task = listBox.SelectedItem as TaskViewModel;
            if (task == null) return;
            var dialog = new CreateTaskDialog(Manager, task){Owner = OwnerWindow};
            var result = dialog.ShowDialog();
            if (result == true)
            {
                var name = dialog.TaskName;
                var descr = dialog.TaskDescription;
                var duration = dialog.TaskDuration;
                CommandsQueue.Do(new ChangeTaskCommand(Manager, task, name, descr, duration));
            }
        }

        private void CommandBinding_EditCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (listBox == null) return;
            var item = listBox.SelectedItem;
            var empl = item as TaskViewModel;
            e.CanExecute = empl != null;
        }
    }
}
